package com.yztc.xiami_app.ui.activity.user;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.yztc.xiami_app.R;
import com.yztc.xiami_app.bean.user.MyUser;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.DownloadFileListener;

/**
 * "用户个人信息"的界面
 */
public class UserInfoActivity extends AppCompatActivity {

    @BindView(R.id.imageview_head)
    ImageView userHeadPicIv; //用户头像
    @BindView(R.id.user_info_userName_tv)
    TextView userNameTv; //用户名
    @BindView(R.id.user_info_neckName_tv)
    TextView neckNameTv; //昵称
    @BindView(R.id.user_info_phone_tv)
    TextView phoneTv; //手机号码
    @BindView(R.id.user_info_email_tv)
    TextView emailTv; //邮箱
    @BindView(R.id.user_info_receive_address_tv)
    TextView receiveAddressTv; //收货地址
    @BindView(R.id.toolbar_message)
    Toolbar toolBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        ButterKnife.bind(this);

        //EventBus的注册
        EventBus.getDefault().register(this);
        initToolBar();
    }

    //初始化ToolBar
    private void initToolBar() {
        toolBar.setTitle("个人信息");
        setSupportActionBar(toolBar);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true); //设置ActionBar默认的返回图标
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
         super.onOptionsItemSelected(item);
        if(item.getItemId() == android.R.id.home){
            finish(); //关闭当前页面
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //接收登录页面传递的登录成功的"User"对象
    @Subscribe(sticky = true)
    public void onEvent(MyUser user) {
        BmobFile headerPicFile = user.getHeaderPic(); //用户头像在Bmob云的的文件
        String username = user.getUsername(); //用户名
        String email = user.getEmail(); //邮箱
        String phone = user.getMobilePhoneNumber();//手机号码
        userNameTv.setText(username);
        emailTv.setText(email);
        phoneTv.setText(phone);
        //显示用户头像
        File saveFile = new File(Environment.getExternalStorageDirectory(), headerPicFile.getFilename());
        headerPicFile.download(saveFile, new DownloadFileListener() {
            @Override
            public void done(String savePath, BmobException e) {
                if (e == null) {
                    showUserHeader(savePath);
                }
            }

            @Override
            public void onProgress(Integer integer, long l) {
            }
        });
    }

    //显示用户头像
    private void showUserHeader(String path) {
        Glide.with(this).load(path)
                .asBitmap()
                .fitCenter()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), resource);
                        roundedBitmapDrawable.setCircular(true); //设置圆形图片
                        userHeadPicIv.setImageDrawable(roundedBitmapDrawable);
                    }
                });
    }
}
